package com.example.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.book.entity.Label;
import com.example.book.entity.Type;
import com.example.book.mapper.TypeMapper;
import com.example.book.service.ITypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 凡哥哥
 * @since 2023-10-16
 */
@Service
@Transactional
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements ITypeService {
      @Autowired
      private TypeMapper typeMapper;

    /**
     * 查询图书类型
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public ResponseResult<Page> pageInfo(Integer page, Integer pageSize, String name) {
        Page<Type> page1 = new Page(page, pageSize);
        LambdaQueryWrapper<Type> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Type::getType, name);
        queryWrapper.eq(Type::getIsDelete,1);
        Page<Type> TypePage = this.page(page1, queryWrapper);
        return ResponseResult.okResult(TypePage);
    }

    /**
     * 添加图书类型
     *
     * @param type
     * @return
     */
    @Override
    public ResponseResult addType(Type type) {
        typeMapper.insert(type);
        return ResponseResult.okResult(type) ;
    }

    /**
     * 数据回显
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult type(Long id) {
        Type type = typeMapper.selectById(id);
        return ResponseResult.okResult(type);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public ResponseResult deleteBooks(Long[] ids) {
        for (Long id : ids) {
            Type type = this.getById(id);
            Boolean status = type.getStatus();
            if (status){
                return ResponseResult.errorResult(500,"标签类型状态正常不能删除");
            }
            LambdaUpdateWrapper<Type> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Type::getId, id);
            updateWrapper.set(Type::getIsDelete, 0);
            this.update(updateWrapper);
        }
        return ResponseResult.okResult("图书类型删除成功");
    }
    /**
     * 修改图书状态
     * @param id
     * @return
     */
    @Override
    public ResponseResult updateStatus(Long id) {
        Type type = typeMapper.selectById(id);
        if (!type.getStatus()) {
            type.setStatus(true);
        } else {
            type.setStatus(false);
        }
        typeMapper.updateById(type);
        return ResponseResult.okResult(type);
    }


}
